In [1]:
%pylab inline
import numpy as np
In [2]:
P = (0.25, 0.50)
idline = np.linspace(0,1,100)
# Identity line y = x, or alternatively Ax + By + C= 0, A =1, B = -1
plt.plot(idline, idline, '--', color='brown') #
plt.plot(P[0], P[1], 'o', color = 'grey', markersize=8)
Out[2]:
In [3]:
def distance(mypoint, myline):
"""
Calculates the distance from a point to a line
"""
x, y = mypoint
A, B, C = myline
return np.abs(A*x + B*y + C) / np.sqrt(np.power(A,2)+np.power(B,2))
In [4]:
mypoint = (5,1)
myline = (3,-1, 1)
distance(mypoint, myline) # 15/np.sqrt(10) = 4.743416
Out[4]:
In [5]:
mypoint = (0.25, 0.50)
myline = (1, -1, 0) # identity line
distance(mypoint, myline)
Out[5]:
In [6]:
def distance_idline(mypoint):
"""
Calculates the distance from a point to the identity line
"""
x, y = mypoint
return np.abs(x-y) / np.sqrt(2)
In [7]:
myfoo = (0.5, 0.5)
distance_idline(myfoo) # must be zero
Out[7]:
In [8]:
distance_idline(mypoint) # must be 0.1767
Out[8]:
In [9]:
mypoint= (0,1)
distance_idline(mypoint) #maximal separation is 1/sqrt(2)
Out[9]:
In [10]:
1/np.sqrt(2) # voila!
Out[10]: